
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[VoteQuestions_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[VoteQuestions_Insert]

IF OBJECT_ID(N'[dbo].[VoteQuestions_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[VoteQuestions_Update]

IF OBJECT_ID(N'[dbo].[VoteQuestions_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[VoteQuestions_Delete]

IF OBJECT_ID(N'[dbo].[VoteQuestions_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[VoteQuestions_Select]

--endregion

GO


--region [dbo].[VoteQuestions_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[VoteQuestions_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdVoteQuestion int = null,
	@Content nvarchar(max) = null,
	@IsActived bit = null,
	@CreatedDate datetime = null,
	@LastModifiedDate datetime = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[VoteQuestions]
					WHERE
					(
						(@IdVoteQuestion is null OR [VoteQuestions].[IdVoteQuestion] = @IdVoteQuestion)
						AND (@Content is null OR [VoteQuestions].[Content] like @Content)
						AND (@IsActived is null OR [VoteQuestions].[IsActived] = @IsActived)
						AND (@CreatedDate is null OR [VoteQuestions].[CreatedDate] = @CreatedDate)
						AND (@LastModifiedDate is null OR [VoteQuestions].[LastModifiedDate] = @LastModifiedDate)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[VoteQuestions].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdVoteQuestion' and @SortType = 1 THEN [VoteQuestions].[IdVoteQuestion] END ASC,
						CASE WHEN @SortBy = 'IdVoteQuestion' and @SortType = 0 THEN [VoteQuestions].[IdVoteQuestion] END DESC,
						CASE WHEN @SortBy = 'Content' and @SortType = 1 THEN [VoteQuestions].[Content] END ASC,
						CASE WHEN @SortBy = 'Content' and @SortType = 0 THEN [VoteQuestions].[Content] END DESC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 1 THEN [VoteQuestions].[IsActived] END ASC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 0 THEN [VoteQuestions].[IsActived] END DESC,
						CASE WHEN @SortBy = 'CreatedDate' and @SortType = 1 THEN [VoteQuestions].[CreatedDate] END ASC,
						CASE WHEN @SortBy = 'CreatedDate' and @SortType = 0 THEN [VoteQuestions].[CreatedDate] END DESC,
						CASE WHEN @SortBy = 'LastModifiedDate' and @SortType = 1 THEN [VoteQuestions].[LastModifiedDate] END ASC,
						CASE WHEN @SortBy = 'LastModifiedDate' and @SortType = 0 THEN [VoteQuestions].[LastModifiedDate] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[VoteQuestions]
			Where 
			(
				(@IdVoteQuestion is null OR [VoteQuestions].[IdVoteQuestion] = @IdVoteQuestion)
				AND (@Content is null OR [VoteQuestions].[Content] like @Content)
				AND (@IsActived is null OR [VoteQuestions].[IsActived] = @IsActived)
				AND (@CreatedDate is null OR [VoteQuestions].[CreatedDate] = @CreatedDate)
				AND (@LastModifiedDate is null OR [VoteQuestions].[LastModifiedDate] = @LastModifiedDate)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[VoteQuestions_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[VoteQuestions_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[VoteQuestions_Insert]
	@IdVoteQuestion int OUTPUT,
	@Content nvarchar(max),
	@IsActived bit,
	@CreatedDate datetime,
	@LastModifiedDate datetime

AS


INSERT INTO [dbo].[VoteQuestions] 
(
	[Content],
	[IsActived],
	[CreatedDate],
	[LastModifiedDate]
)
VALUES 
(
	@Content,
	@IsActived,
	@CreatedDate,
	@LastModifiedDate
)

SET @IdVoteQuestion = SCOPE_IDENTITY()

--end [dbo].[VoteQuestions_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[VoteQuestions_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[VoteQuestions_Update]
	@IdVoteQuestion int,
	@Content nvarchar(max),
	@IsActived bit,
	@CreatedDate datetime,
	@LastModifiedDate datetime
AS


UPDATE [dbo].[VoteQuestions] SET
	[Content] = @Content,
	[IsActived] = @IsActived,
	[CreatedDate] = @CreatedDate,
	[LastModifiedDate] = @LastModifiedDate
WHERE
	[IdVoteQuestion] = @IdVoteQuestion

--end [dbo].[VoteQuestions_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[VoteQuestions_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[VoteQuestions_Delete]
	@IdVoteQuestion int
AS


DELETE FROM [dbo].[VoteQuestions]
WHERE
(
	[IdVoteQuestion] = @IdVoteQuestion
)

--end [dbo].[VoteQuestions_Delete]
--endregion

GO
--=========================================================================================--

